package cn.kgc.app.ssm.controller;

import cn.kgc.app.ssm.common.Const;
import cn.kgc.app.ssm.common.ServerResponse;
import cn.kgc.app.ssm.model.pojo.Backend_user;
import cn.kgc.app.ssm.service.Backend_userService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;


@Controller
@RequestMapping("user")
public class UserController {

    @Resource
    private Backend_userService backendUserService;



    @RequestMapping("getDate")
    @ResponseBody
    public  String getDate(@RequestParam("date") Date date){
        return date.toString();
    }

    @RequestMapping("tz")
    public  ModelAndView xg(){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("backendlogin");
        return modelAndView;
    }

    @RequestMapping("dologin")
    public  ModelAndView userLogin(@RequestParam("userCode")  String userCode,
                                   @RequestParam("userPassword")  String userPassword,
                                   HttpSession session){

        ModelAndView modelAndView=new ModelAndView();
        ServerResponse serverResponse=backendUserService.userLogin(userCode, userPassword);
        if (serverResponse.getCode()<0){
            modelAndView.addObject(Const.ERROR,serverResponse.getMsg());
            modelAndView.setViewName("backendlogin");
        }else {
            session.setAttribute(Const.CURRENT_USER,serverResponse.getData());
            modelAndView.setViewName("backend/main");
        }
        return modelAndView;

    }
}
